package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Util.DateUtil;
import com.hyb.Vo.SaleStaffVo;
import com.hyb.entity.Sale;
import com.hyb.mapper.SaleMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.SaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  销售机会Service接口实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale> implements SaleService {

    @Autowired
    private SaleMapper saleMapper;

    @Override
    public void createSale(Sale sale) {
        DateTimeFormatter fmt= DateTimeFormatter.ofPattern("yyyy-MM-dd");
        sale.setSaleDate(LocalDate.parse(DateUtil.getLocalDate(),fmt));
        sale.setSaleState("未指派");
        saleMapper.insert(sale);
    }

    @Override
    public PageReq getAllSales(Integer limit, Integer current, Integer staffId, String saleType) {
        Page<Sale> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        if (staffId > 0) {
            voQuery.setStaffId(staffId);
        }
        if (!StringUtils.isEmpty(saleType)) {
            voQuery.setSaleType(saleType);
        }
        IPage<Sale> saleIPage = saleMapper.selectAllSales(page, voQuery);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(saleIPage.getTotal());
        pageReq.setMyData(saleIPage.getRecords());
        return pageReq;
    }

    @Override
    public PageReq getSalesByAccountManagers(Integer limit, Integer current, Integer staffId, String saleType) {
        Page<Sale> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        voQuery.setStaffId(staffId);
        voQuery.setSaleCreater(staffId);
        if (!StringUtils.isEmpty(saleType)) {
            voQuery.setSaleType(saleType);
        }

        IPage<Sale> sale = saleMapper.selectSalesByAccountManagers(page, voQuery);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(sale.getTotal());
        pageReq.setMyData(sale.getRecords());
        return pageReq;
    }

    @Override
    public SaleStaffVo getSaleBySaleId(Integer saleId) {
        return saleMapper.selectSaleStaffBySaleId(saleId);
    }

    @Override
    public void assignAccountManager(Sale sale) {
        DateTimeFormatter fmt= DateTimeFormatter.ofPattern("yyyy-MM-dd");
        sale.setSaleAssignDate(LocalDate.parse(DateUtil.getLocalDate(),fmt));
        sale.setSaleState("已指派");
        saleMapper.assignAccountManager(sale);
    }

    @Override
    public void updateSale(Sale sale) {
        saleMapper.updateById(sale);
    }

    @Override
    public void deleteSale(Integer saleId) {
        saleMapper.deleteById(saleId);
    }

    @Override
    public PageReq getAllAssignedSales(Integer limit, Integer current) {
        Page<SaleStaffVo> page = new Page<>(current,limit);
        IPage<SaleStaffVo> saleStaffVo = saleMapper.selectAllAssignedSales(page);
        PageReq pageReq = new PageReq();
        pageReq.setMyData(saleStaffVo.getRecords());
        pageReq.setTotal(saleStaffVo.getTotal());
        return pageReq;
    }

    @Override
    public PageReq getAllAssignedSalesByAccountManager(Integer limit, Integer current, Integer staffId) {
        Page<SaleStaffVo> page = new Page<>(current,limit);
        IPage<SaleStaffVo> saleStaffVoIPage = saleMapper.selectAllAssignedSalesByAccountManager(page, staffId);
        PageReq pageReq = new PageReq();
        pageReq.setTotal(saleStaffVoIPage.getTotal());
        pageReq.setMyData(saleStaffVoIPage.getRecords());
        return pageReq;
    }

    @Override
    public void completeDevelopment(Integer saleId) {
        saleMapper.updateSaleOfComplete(saleId);
    }

    @Override
    public void terminationOfDevelopment(Integer saleId) {
        saleMapper.updateSaleOfTermination(saleId);
    }

}
